package com.topboya.linker.ui.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.topboya.linker.R;
import com.topboya.linker.model.response.MyNoticeListResponse;
import com.topboya.linker.ui.adapter.NotifyAdapter;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.base.fragment.BaseFragment;
import com.topboya.linker.ui.presenter.MyNotifyPresenter;
import com.topboya.linker.ui.view.NotifyView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class NotifyNoticeFragment extends BaseFragment<NotifyView, MyNotifyPresenter> implements NotifyView {

    private static final String TAG = "NotifyNoticeFragment";
    private RecyclerView ryc_notify;
    private List<MyNoticeListResponse.MsgList> msgLists;
    private NotifyAdapter notifyAdapter ;
    private SmartRefreshLayout smart_refreshlayout;
    private int pageNo;

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_notify;
    }

    @Override
    protected void initView() {

        ryc_notify =mRootView.findViewById(R.id.ryc_notify);
        smart_refreshlayout =mRootView.findViewById(R.id.smart_refreshlayout);
        smart_refreshlayout.setEnableRefresh(false);//禁止刷新
    }

    @Override
    protected void initData() {
        pageNo =1;
        initRecycleView();

    }

    private void initRecycleView() {

        mPresenter.getNoticeList(String.valueOf(pageNo));
        msgLists = new ArrayList<>();
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        ryc_notify.setLayoutManager(layoutManager);
        ryc_notify.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        notifyAdapter = new NotifyAdapter(getActivity(),msgLists);
        ryc_notify.setAdapter(notifyAdapter);

    }

    @Override
    public void initListener() {


        smart_refreshlayout.setOnLoadmoreListener(refreshlayout -> {

            pageNo++;
            mPresenter.getNoticeList(String.valueOf(pageNo));


        });
    }
    @Override
    protected MyNotifyPresenter createPresenter() {

        return new MyNotifyPresenter((BaseActivity) getActivity());
    }

    @Override
    public void getMyNoticeList(MyNoticeListResponse.Data mynoticelist) {



    }

    @Override
    public void getNoticeList(MyNoticeListResponse.Data mynoticelist) {

        LogUtils.v(TAG + "mynoticelist:"+mynoticelist.getList().toString());
        List<MyNoticeListResponse.MsgList> msgLists = mynoticelist.getList();
        if (null !=msgLists){

            this.msgLists.addAll(msgLists);
            notifyAdapter.notifyDataSetChanged();
            smart_refreshlayout.finishLoadmore();
        }
    }
}
